{% extends 'main_hub/base.html' %}

{% block title %}
    用户详情
{% endblock %}

{% block nav %}
{% endblock %}

{% block content %}
    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <img src="{{ MEDIA_URL }}{{ other_user.image }}"
                     style="width: 100px; height: 100px;border-radius: 50px">
                <h5 style="display: inline">{% if other_user.nick_name %}
                    {{ other_user.nick_name }}
                {% else %}
                    {{ other_user.username }}
                {% endif %}</h5>
            </div>
            <div class="col-md-6 align-self-center text-center">
                <button type="button" class="btn btn-primary" {% if other_user.id == request.user.id %}disabled{% endif %}
                        onclick="add_user($('#status'),{{ other_user.id }},2)" style="width: 180px;height:35px">
                    {% if fav_user %}
                        <span id="status">已关注</span>
                        <span id="fans_num">{{ other_user.fans }}</span>
                    {% else %}
                        <span id="status">关注</span>
                        <span id="fans_num">{{ other_user.fans }}</span>
                    {% endif %}
                </button>
                <button type="button" class="btn btn-primary" style="width: 180px;height:35px" {% if other_user.id == request.user.id %}disabled{% endif %}
                   onclick="window.open('{% url 'user_message' %}?recipient_username={{ other_user.username }}')">
                    发消息
                </button>
            </div>
        </div>
        <hr>

        <h4>TA的视频</h4>
        <div class="row">
            {% for video in all_video.object_list %}
                <div style="margin: 10px">
                    <div class="card" style="width:12rem;">
                        <a href="{% url 'video_detail' video.id %}?video_sub_number=1">
                            <img class="card-img-top" src="{{ MEDIA_URL }}{{ video.image }}" style="width: 12rem">
                        </a>
                        <div class="card-body">
                            <h5 class="card-title info">
                                <a href="{% url 'video_detail' video.id %}?video_sub_number=1"
                                   style="text-decoration:none;color:black;font-size: 15px;font-weight: bold">
                                    {% if video.name|length > 8 %}
                                        {{ video.name|slice:'0:8' }}...
                                    {% else %}
                                        {{ video.name }}
                                    {% endif %}</a>
                            </h5>
                            <p class="card-text">
                                {% if video.info|length > 9 %}
                                    {{ video.info|slice:'0:8' }}...
                                {% else %}
                                    {{ video.info }}
                                {% endif %}</p>
                        </div>
                    </div>
                </div>
            {% endfor %}
        </div>
        {#    分页#}
        <div class="row justify-content-center">
            <div class="col-auto" style="margin-top: 50px">
                <nav aria-label="Page navigation example">
                    <ul class="pagination" style="right:500px">
                        {% if all_video.has_previous %}
                            <li class="page-item">
                                <a href="?{{ all_video.previous_page_number.querystring }}"
                                   class="page-link" aria-label="Previous">
                                    上一页
                                </a>
                            </li>
                        {% endif %}
                        {% for page in all_video.pages %}
                            {% if page %}
                                {% ifequal page all_video.number %}
                                    <li class="page-item">
                                        <a class="page-link" href="?{{ page.querystring }}">{{ page }}</a>
                                    </li>
                                {% else %}
                                    <li>
                                        <a class="page-link page" href="?{{ page.querystring }}">{{ page }}</a>
                                    </li>
                                {% endifequal %}
                            {% else %}
                                <li class="none">...</li>
                            {% endif %}
                        {% endfor %}
                        {% if all_video.has_next %}
                            <li class="page-item">
                                <a href="?{{ all_video.next_page_number.querystring }}" class="page-link"
                                   aria-label="Next">下一页</a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
            </div>
        </div>
    </div>

{% endblock %}


{% block custom_jss %}
    <script>
        function add_user(current_elem, fav_id, fav_type) {
            $.ajax({
                cache: false,
                type: "POST",
                url: '{% url 'add_fav' %}',
                data: {'fav_id': fav_id, 'fav_type': fav_type},
                async: true,
                beforeSend: function (xhr, setting) {
                    xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                },
                success: function (data) {
                    if (data.status == 'fail') {
                        if (data.msg == '用户未登录') {
                            alert(data.msg)
                            $('#login_show_button').click();
                        } else {
                            alert(data.msg)
                        }
                    } else if (data.status == 'success') {
                        current_elem.text(data.msg);
                        $('#fans_num').text(data.num)
                    }
                }
            })
        }
    </script>
{% endblock %}